package com.kolon.business.mServiceDesk_.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk_.controller.MServiceDeskApplyViewController")
public class MServiceDeskApplyViewController implements SimpleController {

	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mServiceDesk_/mServiceDeskApplyView.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		String SeqNo = ComUtil.nullChk(request.getParameter("SeqNo"));
		String result =  ComUtil.nullChk(request.getParameter("result"));
		String tid = id;
		//권한을 새션에 호출
		HttpSession session = request.getSession(true);
		String assignee = (String)session.getAttribute("SESSION_ASSIGNEE");
		String approval = (String)session.getAttribute("SESSION_APPROVAL");
		String returnUrl = "/business/mServiceDesk_/mServiceDeskApplyList.do";
		ModelAndView modelAndView = new ModelAndView();
		//#######################################################################################
		// 요청 등록 처리
		//#######################################################################################
		if(!result.equals("")) {
		//#######################################################################################
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append(url);
			buffer2.append("/servicedesk.aspx?mode=").append("action");
			buffer2.append("&actionMode=").append("5");
			buffer2.append("&seqno=").append(SeqNo);
			buffer2.append("&result=").append(request.getParameter("result"));
			buffer2.append("&appgubun=").append(request.getParameter("appgubun"));
			buffer2.append("&appstatus=").append(request.getParameter("appstatus"));
			buffer2.append("&comment=").append(request.getParameter("comment"));
			buffer2.append("&userid=").append(tid);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer2.toString(), id, pw);
			modelAndView.addObject("message", "등록되었습니다.");
			modelAndView.addObject("scriptType", "003");
			modelAndView.addObject("message_url", returnUrl + "?page="+page+"&endY="+request.getParameter("endY"));
			modelAndView.setViewName("/business/common/alert");
		//#######################################################################################
		}
		//#######################################################################################
		// 리턴
		modelAndView.addObject("assignee", assignee);
		modelAndView.addObject("approval", approval);
		modelAndView.addObject("page", page);
		modelAndView.addObject("SeqNo", SeqNo);		
		modelAndView.addObject("REQNAME", ComUtil.getDecode(ComUtil.nullChk(request.getParameter("REQNAME")),"UTF-8"));
		modelAndView.addObject("REQDATE", ComUtil.nullChk(request.getParameter("REQDATE")));
		modelAndView.addObject("DETAIL", ComUtil.getDecode(ComUtil.nullChk(request.getParameter("DETAIL")),"UTF-8"));
		//modelAndView.addObject("REQGUBUN", ComUtil.nullChk(request.getParameter("REQGUBUN")));
		modelAndView.addObject("APPGUBUN", ComUtil.nullChk(request.getParameter("APPGUBUN")));
		modelAndView.addObject("APPSTATUS", ComUtil.nullChk(request.getParameter("APPSTATUS")));
		return modelAndView;
	}

}